import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_shop/eventbus/event_bus.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/base/i_contract.dart';
import 'package:flutter_shop/utils/loading.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:flutter_shop/widget/page_state_widget.dart';
import 'package:get_it/get_it.dart';

///Title:BaseState <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-03-05 <br/>
///version 1.0 <br/>
abstract class BaseState<T extends StatefulWidget, P extends BasePresenter> extends State<T> implements IView {
  P mPresenter;

  ///页面状态控制器
  PageStateController pageStateController = PageStateController();

  ///eventbus订阅
  StreamSubscription eventbusSubscription;

  BaseState() {
    mPresenter = createPresenter();
    mPresenter?.mView = this;
    //注入presenter
    if (!P.toString().startsWith("BasePresenter") && !GetIt.instance.isRegistered(instance: mPresenter)) {
      GetIt.instance.registerSingleton<P>(mPresenter, signalsReady: true);
    }
  }

  ///创建presenter
  P createPresenter() => null;

  @override
  void initState() {
    super.initState();
    mPresenter?.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      buildComplete();
      mPresenter?.buildComplete();
    });

    if (useEventBus) {
      eventbusSubscription = EventBus().onEvent().listen((event) {
        onEventBus(event);
      });
    }
  }

  ///构建完成
  void buildComplete() {}

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    mPresenter?.didChangeDependencies();
  }

  @override
  void didUpdateWidget(T oldWidget) {
    super.didUpdateWidget(oldWidget);
    mPresenter?.didUpdateWidget(oldWidget);
  }

  @override
  void deactivate() {
    super.deactivate();
    mPresenter?.deactivate();
  }

  @override
  void dispose() {
    super.dispose();
    cancelLoading();
    mPresenter?.dispose();
    eventbusSubscription?.cancel();
    //解除注入presenter
    if (mPresenter != null) {
      if (GetIt.instance.isRegistered(instance: mPresenter)) {
        GetIt.instance.unregister(instance: mPresenter);
      }
      mPresenter = null;
    }
  }

  ///使用eventbus
  bool get useEventBus => false;

  ///eventbus消息接收
  void onEventBus(EventMessage event) {}

  @override
  BuildContext getContext() {
    return context;
  }

  @override
  void showLoading({String msg = "加载中...", bool canDimiss = false, LoadingWidgetStateController controller}) {
    Loading.showLoading(msg: msg, canDimiss: canDimiss, controller: controller);
  }

  @override
  void cancelLoading() {
    Loading.cancelLoading();
  }

  @override
  void showToast(String str) {
    Utils.showToast(str);
  }

  @override
  void showError(String str) {
    pageStateController.showError(str);
    showToast(str);
  }

  @override
  void setState(fn) {
    if (this.mounted) {
      super.setState(fn);
    }
  }

  @override
  void updateStateWidget() {
    this.setState(() {});
  }
}
